package com.cwl.es.controller;

import com.cwl.es.domain.NBAPlayer;
import com.cwl.es.service.NBAPlayerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @program: cwl_es
 * @description:
 * @author: ChenWenLong
 * @create: 2019-11-05 19:44
 **/
@RestController
@RequestMapping("/nba")
public class NBASearchController {

    @Autowired
    private NBAPlayerService nbaPlayerService;

    /**
     * 功能描述:
     * 〈初始化接口〉
     *
     * @params : []
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/12/3 10:57
     */
    @RequestMapping("/importAll")
    public String importAll(){
        try {
            nbaPlayerService.importAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    /**
     * 功能描述:
     * 〈根据名称全部查询〉
     *
     * @params : [displayNameEn]
     * @return : java.util.List<com.cwl.es.domain.NBAPlayer>
     * @author : cwl
     * @date : 2019/12/3 10:57
     */
    @RequestMapping("/searchMatch")
    public List<NBAPlayer> searchMatch(@RequestParam(value = "displayNameEn", required = false) String displayNameEn) {
        try {
            return nbaPlayerService.searchMatch("displayNameEn",displayNameEn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 功能描述:
     * 〈根据关键字查询〉
     *
     * @params : [country, teamName]
     * @return : java.util.List<com.cwl.es.domain.NBAPlayer>
     * @author : cwl
     * @date : 2019/12/3 10:57
     */
    @RequestMapping("/searchTerm")
    public List<NBAPlayer> searchTerm(@RequestParam(value = "country", required = false) String country,
                                      @RequestParam(value = "teamName", required = false) String teamName) {
        try {
            if(StringUtils.isNoneBlank(country))
                return nbaPlayerService.searchTerm("country",country);

            else if(StringUtils.isNoneBlank(teamName))
                return nbaPlayerService.searchTerm("teamName",teamName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/searchMatchPrefix")
    public List<NBAPlayer> searchMatchPrefix(@RequestParam(value = "displayNameEn", required = false) String displayNameEn) {
        try {
            return nbaPlayerService.searchMatchPrefix("displayNameEn",displayNameEn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
